<template>
  <div class="item-config">
    <el-form
        label-position="top"
        label-width="100px"
        :model="sentineloneConfigForm"
        ref="fieldeffect-form"
    >
      <el-form-item label="Management URL" prop="managementUrl">
        <el-input
            v-model="sentineloneConfigForm.managementUrl" type="text"
            :placeholder="$t('custom.feedback.emailPlaceholder')"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="API Token" prop="apiToken">
        <el-input
            v-model="sentineloneConfigForm.apiToken"
            :type="SecretKeyCheck?'text':'password'"
            :placeholder="$t('custom.connector.solarwindcentral.interpassWord')"
        >
          <i slot="suffix" :class="SecretKeyCheck?'cuifont icon_eye_show':'cuifont icon_eye_hide'"
             @click="SecretKeyCheck=!SecretKeyCheck"></i>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button
            type="primary"
            :loading="testLoading"
            style="width:165px;margin-top: 6px;"
            size="small"
            @click="startTestConfig"
        >{{ $t('custom.connector.savequickbookspath') }}</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { Component } from './modules/Component.js'
import Trail from '@/components/storybook/mixins/trail'
export default {
  name: "SentinelOneConfig",
  mixins: [Trail],

  props: {
    sentineloneConfigForm: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },

  data () {
    return {
      SecretKeyCheck: false,
      testLoading: false
    }
  },
  methods: {
    cancelThisConfig () {
      this.sentineloneConfigForm = {
        apiToken: '',
        managementUrl: ''
      }
    },
    startTestConfig () {
      this.validateConfigForm().then(() => {
        this.testLoading = true
        const params = {
          ...this.sentineloneConfigForm,
          tenantId: this.$store.state.user.tenantCode,
          subjectCode: 'SENTINELONE'
        }
        this.$interface.taskManager
            .postClientData(params)
            .then(({ data: res }) => {
              if (res.code !== 0) {
                this.testLoading = false
                Component.$emit('getConnectorConfig')
                return this.$message.error(res.msg)
              }
              this.testLoading = false
              this.synctdData()
              return this.$message.success(res.msg)
            }).catch(() => {
          this.testLoading = false
        })
      })
    },
    validateConfigForm () {
      return this.$refs['fieldeffect-form'].validate()
    },
    synctdData (itemInfo) {
      const params = {
        tenantId: this.$store.state.user.tenantCode,
        subjectCode: 'SENTINELONE'
      }
      this.$interface.taskManager.syncClientData(params).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }

        if (itemInfo && res.data && res.data.sync) {
          itemInfo.sync.lastTime = res.data.sync.lastTime
          itemInfo.sync.status = res.data.sync.status
        }
        Component.$emit('getConnectorConfig')
        return this.$message.success(res.msg)
      }).catch(() => {
      })
    },
    detail () {

    }
  }
}
</script>

<style scoped>

</style>